{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.17 \u4f20\u9012\u6587\u4ef6\u540d\u7ed9C\u6269\u5c55\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u5411C\u5e93\u51fd\u6570\u4f20\u9012\u6587\u4ef6\u540d\uff0c\u4f46\u662f\u9700\u8981\u786e\u4fdd\u6587\u4ef6\u540d\u6839\u636e\u7cfb\u7edf\u671f\u671b\u7684\u6587\u4ef6\u540d\u7f16\u7801\u65b9\u5f0f\u7f16\u7801\u8fc7\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5199\u4e00\u4e2a\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u540d\u4e3a\u53c2\u6570\u7684\u6269\u5c55\u51fd\u6570\uff0c\u5982\u4e0b\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_get_filename(PyObject *self, PyObject *args) {\n  PyObject *bytes;\n  char *filename;\n  Py_ssize_t len;\n  if (!PyArg_ParseTuple(args,\"O&\", PyUnicode_FSConverter, &bytes)) {\n    return NULL;\n  }\n  PyBytes_AsStringAndSize(bytes, &filename, &len);\n  /* Use filename */\n  ...\n\n  /* Cleanup and return */\n  Py_DECREF(bytes)\n  Py_RETURN_NONE;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2a PyObject * \uff0c\u5e0c\u671b\u5c06\u5176\u8f6c\u6362\u6210\u4e00\u4e2a\u6587\u4ef6\u540d\uff0c\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "PyObject *obj;    /* Object with the filename */\nPyObject *bytes;\nchar *filename;\nPy_ssize_t len;\n\nbytes = PyUnicode_EncodeFSDefault(obj);\nPyBytes_AsStringAndSize(bytes, &filename, &len);\n/* Use filename */\n...\n\n/* Cleanup */\nPy_DECREF(bytes);\n\nIf you need to return a filename back to Python, use the following code:\n\n/* Turn a filename into a Python object */\n\nchar *filename;       /* Already set */\nint   filename_len;   /* Already set */\n\nPyObject *obj = PyUnicode_DecodeFSDefaultAndSize(filename, filename_len);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ee5\u53ef\u79fb\u690d\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u540d\u662f\u4e00\u4e2a\u5f88\u68d8\u624b\u7684\u95ee\u9898\uff0c\u6700\u540e\u4ea4\u7531Python\u6765\u5904\u7406\u3002\n\u5982\u679c\u4f60\u5728\u6269\u5c55\u4ee3\u7801\u4e2d\u4f7f\u7528\u672c\u8282\u7684\u6280\u672f\uff0c\u6587\u4ef6\u540d\u7684\u5904\u7406\u65b9\u5f0f\u548c\u548cPython\u4e2d\u662f\u4e00\u81f4\u7684\u3002\n\u5305\u62ec\u7f16\u7801/\u754c\u9762\u5b57\u8282\uff0c\u5904\u7406\u574f\u5b57\u7b26\uff0c\u4ee3\u7406\u8f6c\u6362\u548c\u5176\u4ed6\u590d\u6742\u60c5\u51b5\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}